
import 'package:dio/dio.dart';

import '../entity/book_entity.dart';
import '../entity/borrow_again_entity.dart';
import '../entity/borrow_entity.dart';
import '../entity/face_search_entity.dart';
import '../entity/nt_face_entity.dart';
import '../entity/reader_entity.dart';
import '../entity/return_entity.dart';
import '../entity/version_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';
import 'api_url.dart';
import 'http_helper.dart';



Future<BookEntity?> findBook(bookNo) async {

  var (status,response) = await HttpHelper.instance.post(findBookUrl, data: {'more': settingEntity.deviceNo, 'book_no': bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = BookEntity.fromJson(response['data']);
      return data;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}

Future<ReaderEntity?> findReader(readerNo, {String category = ''}) async {
  var (status,response) = await HttpHelper.instance.post(findReaderUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'password':'000000','category':category});

  if(status == 200){
    if(response['status'] == 200){
      var data  = ReaderEntity.fromJson(response['data']);
      return data;
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn("${response['error']}");
    return null;
  }
}


Future readerApply(readerNo,name,id) async {
  var (status,response) = await HttpHelper.instance.post(readerApplyUrl,
      data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'reader_name':name,'icr_no':id, 'password':'000000','uid':readerNo});

  if(status == 200){
    return true;
  }else{
    toastWarn("${response['error']}");
    return false;
  }

}

Future bookBorrow(readerNo,bookNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});

  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    // toastWarn("${response['message']}");
    return (400,response['error']);
  }

}

Future bookReturn(bookNo) async {
  var (status,response) = await HttpHelper.instance.post(returnUrl, data: {'more': settingEntity.deviceNo,'book_no':bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = ReturnEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    // toastWarn("${response['message']}");
    return (400,response['error']);
  }
}

Future bookBorrowAgain(readerNo,bookNo) async {
  var (status,response) = await HttpHelper.instance.post(borrowAgainUrl, data: {'more': settingEntity.deviceNo, 'reader_no': readerNo,'book_no':bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = BorrowAgainEntity.fromJson(response['data']);
      return (200,data);
    }
    return (400,response['error']);
  }else {
    return (400, response['error']);
  }
}

Future faceAdd(userId,userName,img) async {
  var (status,response) = await HttpHelper.instance.facePost(faceAddUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),'user_id': userId, 'user_name': userName,'image_base64':img});
  if(status == 200){
    return (true,null);
  }else{
    return (false, response['error']);
  }

}
Future faceSearch(img) async {
  var (status,response) = await HttpHelper.instance.facePost(faceSearchUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),'image_base64':img});
  if(status == 200){
    var data  = FaceSearchEntity.fromJson(response);
    return (data,null);
  }else{
    return (null, response['error']);
  }

}
Future<bool> faceQuery(userId) async {
  var (status,response) = await HttpHelper.instance.facePost(faceQueryUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),"user_id": userId});
  if(status == 200){
    bool isEmpty  = response['empty'];
    return !isEmpty;
  }else{
    toastWarn('${response['error']}');
    return false;
  }

}


Future ntFaceSearch(img,token) async {
  var (status,response) = await HttpHelper.instance.post(ntFaceSearchUrl, data: {'base64Content':img}
      ,options: Options(headers: {'Authorization': token,}));
  if(status == 200){
    var code  = response['error_code'];
    if(code == 0){
      List<dynamic> data = response['result']['user_list'];
      List<NtFaceEntity> list = [];
      for (var element in data) {
        var item = NtFaceEntity.fromJson(element);
        list.add(item);
      }
      if(list.isEmpty){
        return (null, '识别失败');
      }else{
        list.sort((a, b) => a.score!.compareTo(b.score!));
        print(list.last.score);
        if(list.last.score! >= (settingEntity.score??70)){
          return (list.last, null);
        }else{
          return (null, '识别失败');
        }
      }

    }else{
      return (null, response['error_msg']);
    }
  }else{
    return (null, 'error');
  }

}
Future getToken(client_id,client_secret,username,password,grant_type) async {
  var (status,response) = await HttpHelper.instance.post(ntTokenUrl, data: {'client_id':client_id,
    'client_secret':client_secret,'username':username,'password':password,'grant_type':grant_type});
  if(status == 200){
    var token  = response['access_token'];
    var type  = response['token_type'];
    return '$type $token';
  }else{
    return '';
  }

}

Future getVersion() async {
  var (status,response) = await HttpHelper.instance.post(versionUrl, data: {'device_id': settingEntity.deviceNo?.replaceAll('device_id=', ''),"app_id": "self32"});
  if(status == 200){
    if(response['status'] == 200){
      return VersionEntity.fromJson(response['data']);
    }
    toastWarn('${response['error']}');
    return null;
  }else{
    toastWarn('${response['error']}');
    return null;
  }
}
